package com.develop.Controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.develop.BeanUtils.ChannelBean;
import com.develop.BeanUtils.MsgType;
import com.develop.BeanUtils.SharedStorage;
import com.develop.vo.AlarmMsgVo;

@Controller
@RequestMapping("/ReqSyncAlarm")
public class ReqSyncAlarm {
	private static final Logger LOG = LoggerFactory.getLogger(ReqSyncAlarm.class);
	// 消息方式同步告警请求
	@PostMapping("/msg")
	@ResponseBody
	String msg(@RequestBody JSONObject jsonParam) {
	//String msg(String cityCode, String firmCode, String reqId, String alarmSeq) {
		Integer alarmSeq = (Integer)jsonParam.get("alarmSeq");
		Integer reqId = (Integer)jsonParam.get("reqId");
		String cityCode = (String)jsonParam.get("cityCode");
		String firmCode = (String)jsonParam.get("firmCode");
		System.out.println("jsonObject:"+alarmSeq+":"+reqId);
		
		StringBuffer msgBodyBuffer = new StringBuffer();
		msgBodyBuffer.append(MsgType.reqSyncAlarmMsg + ";");
		msgBodyBuffer.append("reqId=" + reqId + ";");
		msgBodyBuffer.append("alarmSeq=" + alarmSeq + ";");

		byte msgType = (byte) MsgType.reqSyncAlarmMsg.getIndex();
		int timeStamp = (int) (System.currentTimeMillis() / 1000L);
		short lenOfBody = (short) msgBodyBuffer.toString().getBytes().length;
		AlarmMsgVo alarmMsgVo = new AlarmMsgVo(msgType, timeStamp, lenOfBody, msgBodyBuffer.toString());

		JSONObject result = sendMessage(cityCode, firmCode, alarmMsgVo);
		return result.toString();
	}

	// 文件方式同步告警请求
	@PostMapping("/file")
	@ResponseBody
	String file(@RequestBody JSONObject jsonParam) {
		
	//json:{"syncSource":1,"firmCode":"HUAWEI","cityCode":"JS","startTime":"2016-04-14 16:25:42","endTime":"2016-04-14 16:25:42","alarmSeq":2151,"reqId":1}	
		Integer alarmSeq = (Integer)jsonParam.get("alarmSeq");
		Integer reqId = (Integer)jsonParam.get("reqId");
		Integer syncSource = (Integer)jsonParam.get("syncSource");
		String startTime = (String)jsonParam.get("startTime");
		String endTime = (String)jsonParam.get("endTime");
		String cityCode = (String)jsonParam.get("cityCode");
		String firmCode = (String)jsonParam.get("firmCode");
		System.out.println("jsonObject:"+alarmSeq+":"+reqId);
		StringBuffer msgBodyBuffer = new StringBuffer();
		msgBodyBuffer.append(MsgType.reqSyncAlarmFile + ";");
		if (StringUtils.isNotBlank(alarmSeq.toString())) {
			// reqSyncAlarmFile;reqId=35;alarmSeq=100;syncSource =1
			msgBodyBuffer.append("reqId=" + reqId + ";");
			msgBodyBuffer.append("alarmSeq=" + alarmSeq + ";");
			msgBodyBuffer.append("syncSource=" + syncSource + ";");
		} else {
			// reqSyncAlarmFile;reqId=33;startTime=2014-11-27 10:00:00;endTime=2014-11-2710:30:00; syncSource=0
			// reqSyncAlarmFile;reqId=34;startTime=2014-11-27 10:00:00;endTime=2014-11-2710:30:00; syncSource=1
			msgBodyBuffer.append("reqId=" + reqId + ";");
			msgBodyBuffer.append("startTime=" + startTime + ";");
			msgBodyBuffer.append("endTime=" + endTime + ";");
			msgBodyBuffer.append("syncSource=" + syncSource + ";");
		}

		byte msgType = (byte) MsgType.reqSyncAlarmFile.getIndex();
		int timeStamp = (int) (System.currentTimeMillis() / 1000L);
		short lenOfBody = (short) msgBodyBuffer.toString().getBytes().length;
		AlarmMsgVo alarmMsgVo = new AlarmMsgVo(msgType, timeStamp, lenOfBody, msgBodyBuffer.toString());

		JSONObject result = sendMessage(cityCode, firmCode, alarmMsgVo);
		return result.toString();
	}

	private JSONObject sendMessage(String cityCode, String firmCode, AlarmMsgVo alarmMsgVo) {
		JSONObject result = new JSONObject();
		int state = 0;
		String msg = "发送成功";

		ChannelBean channelBean = SharedStorage.getChannelBean(cityCode, firmCode);
		if (channelBean != null) {
			channelBean.getChannel().writeAndFlush(alarmMsgVo);
			LOG.info(alarmMsgVo.toString());
		} else {
			state = 1;
			msg = "无法连接通信网关";
		}
		result.put("state", state);
		result.put("msg", msg);

		return result;
	}
}
